package com.athbsd.exer4;

import java.util.Comparator;
import java.util.Objects;

/**
 * Date:2025/2/24
 * Author：zhouzhou
 * Description:
 */
public class Student implements Comparable{
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return Objects.equals(name, student.name) && Objects.equals(age, student.age);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    /**
     *自然排序:如果年龄相同,就比较名字.
     * @param o the object to be compared.
     * @return
     */
    @Override
    public int compareTo(Object o) {
        if(this==o){
            return 0;
        }
        if(o instanceof Student student){
            int value= this.age-student.age;
            if(value!=0){return value;}
            //比较名字
            return this.name.compareTo(student.name);
        }

        throw new RuntimeException("类型不匹配");
    }
}
